package cn.tedu.ivos.audit.service.impl;

import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Transactional
@Service
public class AuditServiceImpl implements AuditService {

    @Autowired(required = false)
    AuditMapper auditMapper;

    @Override
    public void insertAudit(Application application) {
        log.debug("生成申请表对应的审批数据:application={}",application);
        //获取审批人id集合【106，103】
        List<Long> userIdList = application.getAuditUserIdList();
        //2.遍历审批人集合，有几个人，生成几条审批数据
        for(int i=0;i<userIdList.size();i++){
            Audit audit = new Audit();
            //设置本条审批单数据的申请表id
            audit.setApplicationId(application.getId());
            //设置审批人id
            audit.setAuditUserId(userIdList.get(i));
            //设置审批次序 直属领导次序为0 上上级次序为1
            audit.setAuditSort(i);
            //设置审批单状态
            if(i==0){
                //0表示第1条审批数据，对应的人是直属领导，审批状态为“待我审核”
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else{
                //1表示第2条审批数据，对应的人是上上级，审批状态为“待他人审核”
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            //设置新增时间
            audit.setCreateTime(new Date());
            //8.审批数据入库
            auditMapper.insert(audit);
        }
    }
}
